package cn.iocoder.an.module.mold.controller.admin.rechecktask;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.rechecktask.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.rechecktask.RecheckTaskDO;
import cn.iocoder.an.module.mold.service.rechecktask.RecheckTaskService;

@Tag(name = "管理后台 - 复检任务")
@RestController
@RequestMapping("/mold/recheck-task")
@Validated
public class RecheckTaskController {

    @Resource
    private RecheckTaskService recheckTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建复检任务")
    @PreAuthorize("@ss.hasPermission('mold:recheck-task:create')")
    public CommonResult<Long> createRecheckTask(@Valid @RequestBody RecheckTaskSaveReqVO createReqVO) {
        return success(recheckTaskService.createRecheckTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新复检任务")
    @PreAuthorize("@ss.hasPermission('mold:recheck-task:update')")
    public CommonResult<Boolean> updateRecheckTask(@Valid @RequestBody RecheckTaskSaveReqVO updateReqVO) {
        recheckTaskService.updateRecheckTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除复检任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:recheck-task:delete')")
    public CommonResult<Boolean> deleteRecheckTask(@RequestParam("id") Long id) {
        recheckTaskService.deleteRecheckTask(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除复检任务")
                @PreAuthorize("@ss.hasPermission('mold:recheck-task:delete')")
    public CommonResult<Boolean> deleteRecheckTaskList(@RequestParam("ids") List<Long> ids) {
        recheckTaskService.deleteRecheckTaskListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得复检任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:recheck-task:query')")
    public CommonResult<RecheckTaskRespVO> getRecheckTask(@RequestParam("id") Long id) {
        RecheckTaskDO recheckTask = recheckTaskService.getRecheckTask(id);
        return success(BeanUtils.toBean(recheckTask, RecheckTaskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得复检任务分页")
    @PreAuthorize("@ss.hasPermission('mold:recheck-task:query')")
    public CommonResult<PageResult<RecheckTaskRespVO>> getRecheckTaskPage(@Valid RecheckTaskPageReqVO pageReqVO) {
        PageResult<RecheckTaskDO> pageResult = recheckTaskService.getRecheckTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecheckTaskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出复检任务 Excel")
    @PreAuthorize("@ss.hasPermission('mold:recheck-task:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecheckTaskExcel(@Valid RecheckTaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecheckTaskDO> list = recheckTaskService.getRecheckTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "复检任务.xls", "数据", RecheckTaskRespVO.class,
                        BeanUtils.toBean(list, RecheckTaskRespVO.class));
    }


    @PutMapping("/mod-status")
    @Operation(summary = "状态修改接口")
    public CommonResult<Boolean> modStatus(@Valid @RequestBody RecheckTaskModStatusVO modStatusVO) {
        return success(  recheckTaskService.modStatus(modStatusVO));
    }

    @PutMapping("/work-report")
    @Operation(summary = "报工接口")
    public CommonResult<Boolean> workreport(@Valid @RequestBody RecheckTaskWorkReportVO workReportVO) {
        return success(  recheckTaskService.workreport(workReportVO));
    }
}